package br.com.fatecpg.cadastro;

import br.com.cpid.cadastro.R;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;


public class Inserir extends Activity{
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.inserir);
	}
	
	public void CadastrarClick(View v){    	
    	EditText txtNome = (EditText)findViewById(R.id.txtNome);
    	EditText txtEmail = (EditText)findViewById(R.id.txtEmail);
    	EditText txttel = (EditText)findViewById(R.id.edittelefone);
    	
    	if(txtNome.getText().toString().length() <=0){
    		txtNome.setError("Preencha o campo Nome");
    		txtNome.requestFocus();
    		
    	} else if(txtEmail.getText().toString().length() <=0){
    		txtEmail.setError("Preencha o campo E-mail");
    		txtEmail.requestFocus();
    		
    	} else if(txttel.getText().toString().length() <=0){
        		txttel.setError("Preencha o campo Telefone");
        		txttel.requestFocus();
        		
        	}
    		
    	 else {
    		try{
	    		SQLiteDatabase db = openOrCreateDatabase("clientes.db", Context.MODE_PRIVATE, null);
	    		
	    		ContentValues ctv = new ContentValues();
	    		ctv.put("nome", txtNome.getText().toString());
	    		ctv.put("email", txtEmail.getText().toString());
	    		ctv.put("telefone", txttel.getText().toString());
	    		
	    		if(db.insert("clientes", "_id", ctv) > 0){
	    			Toast.makeText(getBaseContext(), "Sucesso ao Cadastrar o Cliente", Toast.LENGTH_SHORT).show();
	    			finish();
	    		} else {
	    			Toast.makeText(getBaseContext(), "Erro ao Cadastrar o Cliente", Toast.LENGTH_SHORT).show();
	    		}
	    		db.close();
    		
    		} catch(Exception ex){
    			Toast.makeText(getBaseContext(), ex.getMessage(), Toast.LENGTH_SHORT).show();
    		}
    	}
    	
	}
}
